﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DNN.Modules.UrlRedirectProvider.Entities
{
    internal class RedirectInfo
    {
     
        public Guid RedirectId {get;set;}
		public int PortalId {get;set;}
		public int RedirectOrder {get;set;}
		public string HttpAlias {get;set;}
		public string RedirectUrl {get;set;}
		public string MatchType {get;set;}
		public bool KeepQueryString {get;set;}
		public string DestTabPath {get;set;}
		public int DestTabId {get;set;}
		public string DestUrl {get;set;}
		public string DestType {get;set;}
		public bool IsRegex {get;set;}

        //derived field, returns the url as generated by the matchType
        internal string MatchUrl
        {
            get
            {
                return GetUrl(MatchType, HttpAlias, RedirectUrl);
            }
        }

        internal bool IsValid(out string validationMessage )
        {
            //validate the url redirect for the ability to be used
            validationMessage = "Invalid Redirect Id [" + RedirectId.ToString() + "] ;";
            int noErrorLength = validationMessage.Length;
            if (string.IsNullOrEmpty(RedirectUrl))
            {
                validationMessage += "Redirect Url Field is null.  Supply a valid Url.  ;";
            }
            if (string.IsNullOrEmpty(MatchType))
            {
                validationMessage += "MatchType field is null.  Use either 'absolute' or 'relative' ;";
            }
            else
            {
                if (MatchType.ToLower() != "absolute" && MatchType.ToLower() != "relative")
                    validationMessage += "MatchType field of '" + MatchType + "' is invalid.  Use either 'absolute' or 'relative' ;";

            }
            if (string.IsNullOrEmpty(DestType))
            {
                validationMessage += "DestType field is null.  Use either 'tab' or 'url' ;";
            }
            else
            {
                if (DestType.ToLower() != "tab" && DestType.ToLower() != "url")
                {
                    validationMessage += "DestType field of '" +  DestType + "' is invalid.  Use either 'tab' or 'url' ;";
                }

                if (DestType.ToLower() == "tab" && (DestTabId <= 0 && string.IsNullOrEmpty(DestTabPath)))
                {
                    validationMessage = "Specify either a valid DestTabId value, or a valid DestTabPath ;";
                }

                if (DestType.ToLower() == "url" && string.IsNullOrEmpty(DestUrl))
                    validationMessage += "DestUrl must be specified for DestType of 'url' ;";
            }
            if (validationMessage.Length > noErrorLength)
                return false;
            else
                return true;
        }


        internal static string GetUrl(string matchType, string httpAlias, string redirectUrl)
        {
            string url = null;
            switch (matchType.ToLower())
            {
                case "relative":
                    url= redirectUrl;
                    break;
                case "absolute":
                    if (string.IsNullOrEmpty(httpAlias))
                        return redirectUrl;
                    else
                    {
                        url = httpAlias;
                        if (url.EndsWith("/") == false)
                            url += "/";
                        if (redirectUrl.StartsWith("/") && redirectUrl.Length > 0)
                            url += redirectUrl.Substring(1);
                        else
                            url += redirectUrl;
                    }
                    break;
            }
            return url;
        }
    }
}
